package com.teacold.customdemo.chapter_ten;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class RectRefreshSurfaceView extends SurfaceView {
    private Paint mPaint;
    public RectRefreshSurfaceView(Context context) {
        this(context,null);
    }

    public RectRefreshSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RectRefreshSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init() {
        mPaint=new Paint();
        mPaint.setColor(Color.argb(0x1f,0xff,0xff,0xff));
        mPaint.setTextSize(30);
        getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                drawText(holder);

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

    }

    private void drawText(final SurfaceHolder holder) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Rect dirtyRect=new Rect(0,0,1,1);
                    Canvas canvas=holder.lockCanvas(dirtyRect);
                    Rect cancasRect=canvas.getClipBounds();
                    Log.e("qqqq","宽："+cancasRect.width()+"高："+cancasRect.height());
                    if(getWidth()==cancasRect.width()&&getHeight()==cancasRect.height()){
                        canvas.drawColor(Color.BLACK);
                        holder.unlockCanvasAndPost(canvas);
                    }else {
                        holder.unlockCanvasAndPost(canvas);
                        break;
                    }
                }
                    //画图
                    for (int i=0;i<10;i++){
                        //画大方
                        if(i==0){
                            Canvas canvas1=holder.lockCanvas(new Rect(10,10,600,600));
                            canvas1.drawColor(Color.RED);
                            holder.unlockCanvasAndPost(canvas1);
                        }
                        //画中方
                        if(i==1){
                            Canvas canvas1=holder.lockCanvas(new Rect(30,30,570,570));
                            canvas1.drawColor(Color.GREEN);
                            holder.unlockCanvasAndPost(canvas1);
                        }
                         //画小方
                        if(i==2){
                            Canvas canvas1=holder.lockCanvas(new Rect(60,60,540,540));
                            canvas1.drawColor(Color.BLUE);
                            holder.unlockCanvasAndPost(canvas1);
                        }
                        //画圆形
                        if(i==3){
                            Canvas canvas1=holder.lockCanvas(new Rect(200,200,400,400));
                            mPaint.setColor(Color.argb(0x3F,0xFF,0xFF,0xFF));
//                            mPaint.setColor(Color.YELLOW);
                            canvas1.drawCircle(300,300,100,mPaint);
                            holder.unlockCanvasAndPost(canvas1);
                        }
                        //写数字
                        if(i==4){
                            Canvas canvas1=holder.lockCanvas(new Rect(250,250,350,350));
                            mPaint.setColor(Color.RED);
                            canvas1.drawText(i+"",300,300,mPaint);
                            holder.unlockCanvasAndPost(canvas1);
                        }
                        try {
                            Thread.sleep(800);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

            }
        }).start();


    }


}
